package com.lovefreedom.psi.action;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.lovefreedom.psi.bean.Category;
import com.lovefreedom.psi.bean.Goods;
import com.lovefreedom.psi.dao.GoodsDao;
import com.lovefreedom.psi.dao.impl.GoodsDaoImpl;
import com.lovefreedom.psi.util.Utils;

public class GoodsAction {
	private int id;
	private String code;	
	private String name;
	private String unit;
	private float beginInventory;	
	private float currentInventory;
	private float purchasePrice;
	private float sellingPrice;
	private String remark;	
	private int categoryId;
	
	private int pageSize=10;
	private int pageIndex=1;

	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageIndex() {
		return pageIndex;
	}
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUnit() {
		return unit;
	}
	public void setUnit(String unit) {
		this.unit = unit;
	}
	public float getBeginInventory() {
		return beginInventory;
	}
	public void setBeginInventory(float beginInventory) {
		this.beginInventory = beginInventory;
	}
	public float getCurrentInventory() {
		return currentInventory;
	}
	public void setCurrentInventory(float currentInventory) {
		this.currentInventory = currentInventory;
	}
	public float getPurchasePrice() {
		return purchasePrice;
	}
	public void setPurchasePrice(float purchasePrice) {
		this.purchasePrice = purchasePrice;
	}
	public float getSellingPrice() {
		return sellingPrice;
	}
	public void setSellingPrice(float sellingPrice) {
		this.sellingPrice = sellingPrice;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public int getCategoryId() {
		return categoryId;
	}
	public void setCategoryId(int categoryId) {
		this.categoryId = categoryId;
	}	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}	

	

	public String list() throws Exception{  
	    System.out.println("现在执行列表操作");  
		HttpServletRequest req = ServletActionContext.getRequest(); 		
		req.setCharacterEncoding("UTF-8");  

		
		//从Cookie中获取分页大小
		Cookie cookie = Utils.getCookieByName(req, "pageSize");
		if(cookie != null){
			pageSize = Integer.parseInt(cookie.getValue());
		}else{
			pageSize=10;
		}		    
		
	    //获取当前页码值
		String page = req.getParameter("page");
		if(page != null) {
			pageIndex = Integer.parseInt(page);
		}else{
			pageIndex = 1;
		}
		
		//获取查询关键字
		String keywords = "";
		if (req.getParameter("txtKeywords") != null){			
			keywords = req.getParameter("txtKeywords");
		}				
		if(req.getParameter("keywords") != null){			
			keywords = new String(req.getParameter("keywords").getBytes("ISO-8859-1"),"UTF-8"); 			
		}
		System.out.println("keywords="+keywords);	
	
		
		//获取分页数据
		GoodsDao dao = new GoodsDaoImpl();	
		int totalCount = dao.getGoodsTotalCount(keywords);
		List list = dao.getGoodsList(pageSize,pageIndex,keywords);		
		
		//生成翻页字符串		
		String pageUrl = "list.action?keywords=" + keywords + "&page=__id__";
		String pageContent = Utils.OutPageList(pageSize, pageIndex, totalCount, pageUrl, 8);				
		req.setAttribute("goodsList", list);
		req.setAttribute("pageContent", pageContent);
		req.setAttribute("pageSize", pageSize);
	    return "list";  
	}
	public String add() throws Exception{  
	    System.out.println("现在执行增加操作");  	    
		GoodsDao dao = new GoodsDaoImpl();	
		Goods goods = new Goods();
		goods.setCode(code);		
		goods.setName(name);
		goods.setUnit(unit);
		goods.setBeginInventory(beginInventory);
		//当前数量等于初始数量
		goods.setCurrentInventory(beginInventory);
		goods.setPurchasePrice(purchasePrice);
		goods.setSellingPrice(sellingPrice);
		goods.setRemark(remark);
		
		Category category = new Category();
		category.setId(categoryId);
		goods.setCategory(category);
		
		dao.addGoods(goods);	
	    return "list";  
	}
	public String delete() throws Exception{  		
	    System.out.println("现在执行删除操作");  		
		HttpServletRequest req = ServletActionContext.getRequest();  
	    String goodsIds[] = req.getParameterValues("goodsIds");  
	    
		GoodsDao dao = new GoodsDaoImpl();		    		
	    for(int i=0;i<goodsIds.length;i++)
	    {	    	
	    	int id = Integer.parseInt(goodsIds[i]); 

	    	//进货单中没有该货品时，该货品才能被删除。
	    	if(1 == 1){
	    		dao.deleteGoods(id);
	    	}	    	
	    }
	    return "list";  
	}	
	
	public String update() throws Exception{  		
	    System.out.println("现在执行修改操作");  
	    GoodsDao dao = new GoodsDaoImpl();	
		Goods goods = new Goods();
		goods.setId(id);
		goods.setCode(code);		
		goods.setName(name);
		goods.setUnit(unit);
		goods.setBeginInventory(beginInventory);
		goods.setCurrentInventory(beginInventory);
		goods.setPurchasePrice(purchasePrice);
		goods.setSellingPrice(sellingPrice);
		goods.setRemark(remark);
		
		Category category = new Category();
		category.setId(categoryId);
		goods.setCategory(category);
			
    	//进货单中没有该货品时，该货品才能被修改。
    	if(1 == 1){
    		dao.updateGoods(goods);	
    	}		
	    return "list";  
	}		
	
	public String importGoods() throws Exception{  		
	    System.out.println("现在执行导入货品操作");  
	    GoodsDao dao = new GoodsDaoImpl();	
		Goods goods = new Goods();
		goods.setId(id);
		goods.setCode(code);		
		goods.setName(name);
		goods.setUnit(unit);
		goods.setBeginInventory(beginInventory);
		goods.setCurrentInventory(beginInventory);
		goods.setPurchasePrice(purchasePrice);
		goods.setSellingPrice(sellingPrice);
		goods.setRemark(remark);
		
		Category category = new Category();
		category.setId(categoryId);
		goods.setCategory(category);
			
    	//进货单中没有该货品时，该货品才能被修改。
    	if(1 == 1){
    		dao.updateGoods(goods);	
    	}		
	    return "list";  
	}		
}

